﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Marbles.Core.Content.Syndication
{
    public class FeedItem : IFeedItem
    {
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
            }
        }

        public string Link
        {
            get
            {
                return _link;
            }
            set
            {
                _link = value;
            }
        }

        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }

        public string Author
        {
            get
            {
                return _author;
            }
            set
            {
                _author = value;
            }
        }

        public DateTime PublishDate
        {
            get
            {
                return _publishdate;
            }
            set
            {
                _publishdate = value;
            }
        }

        private string _title = string.Empty;
        private string _link = string.Empty;
        private string _description = string.Empty;
        private string _author = string.Empty;
        private DateTime _publishdate = Marbles.Core.Connectors.DataConnection.MinimumDateTime;

        public FeedItem()
        {
            _title = string.Empty;
            _link = string.Empty;
            _description = string.Empty;
            _author = string.Empty;
            _publishdate = Marbles.Core.Connectors.DataConnection.MinimumDateTime;
        }

        public FeedItem(string Title, string Link, string Description, string Author, DateTime PublishDate)
            : this()
        {
            _title = Title;
            _link = Link;
            _description = Description;
            _author = Author;
            _publishdate = PublishDate;
        }

        public FeedItem(string Title, Uri Link, string Description, string Author, DateTime PublishDate)
            : this()
        {
            _title = Title;
            _link = Link.OriginalString;
            _description = Description;
            _author = Author;
            _publishdate = PublishDate;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<item>");
            sb.AppendFormat("<title>{0}</title>", HttpUtility.HtmlEncode(Title));
            sb.AppendFormat("<link>{0}</link>", HttpUtility.HtmlEncode(Link));
            sb.AppendFormat("<author>{0}</author>", HttpUtility.HtmlEncode(Author));
            sb.AppendFormat("<description>{0}</description>", HttpUtility.HtmlEncode(Description));
            sb.AppendFormat("<pubDate>{0}</pubDate>", PublishDate.ToString("r"));
            sb.Append("</item>");
            return sb.ToString();
        }
    }
}
